package net.uman.framework.modules.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import net.uman.framework.io.FileManagerFactory;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.framework.servlets.StoreOrderType;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;

public class UploadFormFileUtils {
	public static List<String> readFiles(HttpServletRequest request, String storeType) {
        List<String> names = new ArrayList<String>();
        
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        
        StoreOrderType sot = new StoreOrderType();
        try {
            String today = new SimpleDateFormat("yyMMdd").format(new Date());

            List items = upload.parseRequest(request);
            Iterator it = items.iterator();
            while (it.hasNext()) {
                FileItem item = (FileItem) it.next();
                String name = sot.StoreFile(storeType, item, today);
                if(name != null){
                	names.add(name);
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return names;
    }
	public static String storeFile(File file,String imgUploadFileName) {
        String newImgUploadFileName=null;
		long now = new Date().getTime();
		String today = new SimpleDateFormat("yyMMdd").format(new Date());
		
//		String path = ServletActionContext.getServletContext().getRealPath("");
//		File dir = new File(path);
		String path = "uploadImages/" + today ;
		String filePath = Struts2Utils.getRequest().getRealPath("/") + "/" + path;
		File dir = new File(filePath);
		if(!dir.exists()){
			 dir.mkdirs();
		}
		
		int index = imgUploadFileName.lastIndexOf(".");
		if (index != -1) {
			newImgUploadFileName = UUID.randomUUID().toString() + imgUploadFileName.substring(index);
		} else {
			newImgUploadFileName = UUID.randomUUID().toString();
		}
		
		InputStream is=null;
		OutputStream os=null;
		File  newfile=null;
        try {
        	newfile = new File(dir, newImgUploadFileName);
    		is = new FileInputStream(file);
    		os = new FileOutputStream(newfile);
    		byte[] buf = new byte[1024];
    		int len = -1;
    		while ((len = is.read(buf)) != -1) {
    			os.write(buf, 0, len);
    		}
    		is.close();
    		os.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
    	   try{
    		   if(null!=is){
    			   is.close();
    		   }
    		   if(null!=os){
    			   os.close();
    		   }
          }catch (Exception e) {
              e.printStackTrace();
          }
        }
        return newfile.getAbsolutePath() ;
    }
	
}
